package jFreeChart;

import java.awt.Font;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * 
 * 柱状图
 */
public class BarChartDemo
{
	public static void main(String[] args) throws IOException
	{
		CategoryDataset dataset = getDataSet2();
		JFreeChart chart = ChartFactory.createBarChart("水果产量图", // 图表标题
				"水果", // 目录轴的显示标签
				"产量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				true, // 是否生成工具
				true // 是否生成URL链接
				);
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 16));

		CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
		domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));
		domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));

		ValueAxis valueAxis = chart.getCategoryPlot().getRangeAxis();
		valueAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));

		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

		ChartFrame frame = new ChartFrame("水果产量图 ", chart, true);
		frame.pack();
		frame.setVisible(true);
	}

	private static CategoryDataset getDataSet()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, null, "苹果");
		dataset.addValue(200, null, "梨子");
		dataset.addValue(300, null, "葡萄");
		dataset.addValue(400, null, "香蕉");
		dataset.addValue(500, null, "荔枝");
		return dataset;
	}

	private static CategoryDataset getDataSet2()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "北京", "苹果");
		dataset.addValue(100, "上海", "苹果");
		dataset.addValue(100, "广州", "苹果");
		dataset.addValue(200, "北京", "梨子");
		dataset.addValue(200, "上海", "梨子");
		dataset.addValue(200, "广州", "梨子");
		dataset.addValue(300, "北京", "葡萄");
		dataset.addValue(300, "上海", "葡萄");
		dataset.addValue(300, "广州", "葡萄");
		dataset.addValue(400, "北京", "香蕉");
		dataset.addValue(400, "上海", "香蕉");
		dataset.addValue(400, "广州", "香蕉");
		dataset.addValue(500, "北京", "荔枝");
		dataset.addValue(500, "上海", "荔枝");
		dataset.addValue(500, "广州", "荔枝");
		return dataset;
	}
}
